﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ConditionalAppearance;

namespace DomainComponents.XCRM {
    [NonPersistent]
    public class CreateOrderFromQuoteParameters {
        public CreateOrderFromQuoteParameters(IQuote quote) {
            this.DateWon = DateTime.Now;
            this.CloseOpportunity = true;
        }
        public DateTime DateWon { get; set; }
        public bool CloseOpportunity { get; set; }
    }
    [DomainLogic(typeof(IQuote))]
    public class Quote_CreateOrder_Logic {
        public static void CreateOrderFromQuote(IQuote quote, CreateOrderFromQuoteParameters parameters) {
            IOrder order = quote.CreateOrder();
            quote.Copy(order);
            quote.Orders.Add(order);
            quote.ClosedOn = parameters.DateWon;
            quote.Status = QuoteStatus.Won;
            if(parameters.CloseOpportunity && order.SourceOpportunity != null) {
                order.SourceOpportunity.Status = OpportunityStatus.Won;
            }
        }
    }
}
